package com.xxh.food.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxh.food.common.PagePARAM;
import com.xxh.food.common.Result;
import com.xxh.food.common.entity.User;
import com.xxh.food.common.param.UserPARAM;
import com.xxh.food.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(value = "/user")
@Api(tags = "用户模块接口")
public class UserController {
    @Resource
    private UserService userService;


    @ApiOperation(value = "添加用户")
    @PostMapping(value = "/add")
    public Result<?> addUser(@RequestBody UserPARAM userPARAM){
        User user =new User();
        BeanUtils.copyProperties(userPARAM,user);
        User olduser = userService.findUserByUsername(userPARAM.getUsername());
        if(olduser!=null) {
            return Result.error("401", "用户名重复");
        }
        userService.save(user);
        return Result.success(user);
    }

    @ApiOperation(value = "更新用户")
    @PutMapping(value = "/update")
    public Result<?> updateUser(@RequestBody UserPARAM userPARAM){
        User user =new User();
        BeanUtils.copyProperties(userPARAM,user);
        userService.saveOrUpdate(user);
        return Result.success(user);
    }

    @ApiOperation(value = "根据Id查询用户")
    @GetMapping(value = "/{id}")
    public Result<?> findUserById(@PathVariable String id){
        User user = userService.getById(id);
        return Result.success(user);
    }

    @ApiOperation(value = "查询用户总数")
    @GetMapping(value = "/num")
    public Result<?> getUserNum(){
        int num = userService.userNum();
        if (num > 0) {
            return Result.success(num);
        } else {
            return Result.error("-1", "查询失败");
        }
    }

    @ApiOperation(value = "根据Id删除用户")
    @ApiImplicitParam(name = "ids",value = "用户id集合",required = true)
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> deleteUserById(@PathVariable String id ){
        userService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public Result<?> findUserByPage(@RequestBody PagePARAM<UserPARAM> pagePARAM){
        Page<User> page = userService.findUserByPage(pagePARAM.getPageNum(),pagePARAM.getPageSize(),pagePARAM.getSearchPARAM());
        return Result.success(page);
    }


    @ApiOperation(value = "用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户账号", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true),
    })
    @PostMapping("/login")
    public Result<?> login(
            @RequestParam("username") String username,
            @RequestParam("password") String password, @ApiIgnore HttpSession session) {
        Object o = userService.login(username,password);
        if (o != null) {
            //存入session
            session.setAttribute("User", o);
            return Result.success(o);
        } else {
            return Result.error("401", "用户名或密码错误");
        }
    }


    @ApiOperation(value = "管理员用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户账号", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true),
    })
    @PostMapping("/adminlogin")
    public Result<?> adminLogin(
            @RequestParam("username") String username,
            @RequestParam("password") String password, @ApiIgnore HttpSession session) {
        Object o = userService.adminLogin(username,password);
        if (o != null) {
            //存入session
            session.setAttribute("User", o);
            return Result.success(o);
        } else {
            return Result.error("401", "用户名或密码错误");
        }
    }

}
